import React from "react"

/**
 * 对于类组件，直接给他设置ref,可以获取该组件创建的实例
 */
class Child extends React.Component {
    state = {
        num: 5,
    }
    render() {
        const { num } = this.state
        return <div ref={(x) => (this.child1 = x)}>{num}</div>
    }
}

class Demo extends React.Component {
    box2 = React.createRef()
    componentDidMount() {
        // console.log(this.refs.titleBox)
        // console.log(this.box1)
        // console.log(this.box2.current)
        console.log(this.child)
    }

    render() {
        return (
            <div ref="titleBox">
                <h1 ref={(x) => (this.box1 = x)}>我是标题</h1>
                <h2 ref={this.box2}>我也是</h2>
                <Child ref={(x) => (this.child = x)} />
            </div>
        )
    }
}

export default Demo
